﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace WebCrawlerTools
{
    class SPart : SNode
    {
        private String src;
        private STemplate stmpl;
        private bool findFirstNode = false;
        private int firstNodeEndPos = -1;
        public SPart()
        {
            this.StartPos= -1;
            this.EndPos = -1;
            type = CNodeType.SPart;
        }

        public SPart(int startPos)
        {
            this.StartPos = startPos;
            this.EndPos = -1;
            type = CNodeType.SPart;
        }

        public override void reset()
        {
            this.StartPos = -1;
            this.EndPos = -1;
        }

        public SLine getValues()
        {
            return SEngine.getSEngine().Template.getValues();
        }

        public bool FindPart(int findStartPos)
        {
            src = SEngine.getSEngine().Source;
            stmpl = SEngine.getSEngine().Template;
            bool find = false;
            int startPos = findStartPos;
            do
            {
                stmpl.Reset();
                find = FindOnePart(startPos);
                if (!find)
                {
                    if (findFirstNode)
                    {
                        startPos = firstNodeEndPos;
                    }
                }
                else
                {
                    return true;
                }

            } while (findFirstNode);
            return false;
        }

        public bool FindOnePart(int findStartPos)
        {
            bool found = false;
            bool preIsVal = false;
            bool isFirstNode = true;
            int startPos = findStartPos;
            this.EndPos = -1;
            findFirstNode = false;
            firstNodeEndPos = -1;
            SNode sn = stmpl.getNextNode();
            SKey preSKey=null;
            SKey sk = null;
            SVal sv=null;
            while (sn != null)
            {
                if (sn.IsKey())
                {
                    sk = (SKey)sn;
                    if (src.Length <= startPos)
                    {
                        break;
                    }
                    if (sk.Find(ref src, startPos))
                    {
                        if (preIsVal)
                        {
                            sv.StartPos = preSKey.EndPos;
                            sv.EndPos = sk.StartPos;
                            sv.Value = src.Substring(sv.StartPos, sv.EndPos - sv.StartPos);
                            if (SNode.URL_TAG == sv.Content)
                            {
                                if (sv.Value.IndexOf(SNode.HTML_TAG_S) != -1 || sv.Value.IndexOf(SNode.HTML_TAG_S) != -1)
                                {
                                    found = false;
                                    break;
                                }
                            }
                        }
                        preSKey = sk;
                        startPos = sk.EndPos;
                        preIsVal = false;
                        if (isFirstNode)
                        {
                            this.StartPos = sk.StartPos;
                            isFirstNode = false;
                            findFirstNode = true;
                            firstNodeEndPos = sk.EndPos;
                        }
                    }
                    else
                    {
                        found = false;
                        break;
                    }
                }
                else
                {
                    sv = (SVal)sn;
                    preIsVal = true;
                }
                sn = stmpl.getNextNode();
                if (sn == null)
                {
                    this.EndPos = sk.EndPos;
                    found = true;
                }
            }
            return found;
        }
    }
}
